// pages/assessmentResults/assessmentResults.js
const Controller = require('../controllers/controllers.js').controller;
import F2 from '@antv/wx-f2'; // 注：也可以不引入， initChart 方法已经将 F2 传入，如果需要引入，注意需要安装 @antv/wx-f2 依赖
const app = getApp();
let chart = null;

var list = []

//数组排序
function compare(property, type) {
  return function (a, b) {
    var value1 = a[property];
    var value2 = b[property];
    let value;
    if (type == "mini") {
      value = value2 - value1;
    } else if (type == "max") {
      value = value1 - value2;
    }
    return value;
  };
}

function initChart(canvas, width, height, F2) { // 使用 F2 绘制图表
  var data = [
    { type: '平\n和', sales: Number(list[0]) },
    { type: '气\n虚', sales: Number(list[1]) },
    { type: '湿\n热', sales: Number(list[2]) },
    { type: '阴\n虚', sales: Number(list[3]) },
    { type: '气\n郁', sales: Number(list[4]) },
    { type: '血\n瘀', sales: Number(list[5]) },
    { type: '阳\n虚', sales: Number(list[6]) },
    { type: '痰\n湿', sales: Number(list[7]) },
    { type: '特\n禀', sales: Number(list[8]) },
    { type: '血\n虚', sales: Number(list[9]) },
    { type: '阴\n寒', sales: Number(list[10]) },
    { type: '阳\n热', sales: Number(list[11]) }
  ];
  data = data.sort(
    compare("sales", "max")
  );
  chart = new F2.Chart({
    el: canvas,
    width,
    height,
    pading: [0, 10, 0, 10]
  });

  chart.source(data, {
    sales: {
      tickCount: 5
    }
  });
  data.forEach((obj,index)=>{
    let color = '#fffe02'
    if (obj.sales <= 20 && obj.sales >= 0) {
      color = '#fffe02';
    } else if (obj.sales <= 40 & obj.sales > 20) {
      color = '#fea828';
    } else if (obj.sales <= 60 && obj.sales > 40) {
      color = '#92651f';
    } else if (obj.sales > 60) {
      color = '#f60400';
    }
    chart.guide().text({
      position: [obj.type, obj.sales > 0 ? obj.sales : 0],
      content: obj.sales,
      style: {
        fill: color,
        textBaseline: 'bottom',
        fontSize:'9'
      },
      offsetY: -5
    });
  })
  chart.tooltip(false);
  chart.axis('sales', {

    position: 'left', // 纵坐标位置
  })
  chart.axis('type', {
    label: {
      textAlign: 'center',
      textBaseline: 'top'
    }
  });
  chart.interval().position('type*sales').color('sales', function (val) {
    if (val <= 20 && val >= 0) {
      return '#fffe02';
    } else if (val <= 40 & val > 20) {
      return '#fea828';
    } else if (val <= 60 && val > 40) {
      return '#92651f';
    } else if (val > 60) {
      return '#f60400';
    }
  });;
  chart.render();
  return chart;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    opts: null,
    id: '',
    detail: null
  },
  formatRichText(html) {
  if(html){
    let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
    });
    newContent = newContent.replace(/<table[^>]*>/gi, function(match, capture) {
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
    });
    newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
      match = match.replace(/width:[^;]+;/gi, 'max-width:100%;vertical-align:middle;')
      return match;
    });
    // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
    newContent = newContent.replace(/\<img/gi,
      '<img style="max-width:100%;vertical-align:middle;"');
    return newContent;
  }
},
  examdetail() {
    Controller.examdetail({
      healthReportId: this.data.id
    }).then(res => {
      if (res.code == 0) {
        let detail = res.data.baseData
        list = detail.scores.split(',')
        detail.resDesc = this.formatRichText(detail.resDesc)
        detail.resProgram = this.formatRichText(detail.resProgram)
        detail.resTips = this.formatRichText(detail.resTips)
        this.setData({
          detail,
          opts: {
            onInit: initChart
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.examdetail()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onShareAppMessage: function (e) {
    let userInfo=wx.getStorageSync('userInfo') || '';
    var parentid='';
    if(userInfo){
      parentid=userInfo.id
    }
    var path="pages/selectMember/selectMember?parentid="+parentid;

    let title = '';
    let imageUrl = '';
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let userInfo=wx.getStorageSync('userInfo') || '';
    var parentid='';
    if(userInfo){
      parentid=userInfo.id
    }
    var path="pages/selectMember/selectMember?parentid="+parentid;

    let title = '';
    let imageUrl = '';
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})